# XML DOM Remove Nodes

« Previous
Next Chapter »

The removeChild() method removes a specified node.

The removeAttribute() method removes a specified attribute.

## Try it Yourself - Examples

The examples below use the XML file books.xml.
A function, loadXMLDoc(), in an external JavaScript is used to load the XML file.

Remove an element node
This example uses removeChild() to remove the first <book> element.

Remove the current element node
This example uses parentNode and removeChild() to remove the current <book> element.

Remove a text node
This example uses removeChild() to remove the text node from the first <title> element.

Clear the text of a text node
This example uses the nodeValue() property to clear the text node of the first <title> element.

Remove an attribute by name
This example uses removeAttribute() to remove the "category" attribute from the first <book> element.

Remove attributes by object
This example uses removeAttributeNode() to remove all attributes from all <book> elements.

## Remove an Element Node

The removeChild() method removes a specified node.

When a node is removed, all its child nodes are also removed.

The following code fragment will remove the first <book> element from the loaded xml:

## Example

xmlDoc=loadXMLDoc("books.xml");
y=xmlDoc.getElementsByTagName("book")[0];
xmlDoc.documentElement.removeChild(y);
Try it yourself »

Example explained:

1. Load "books.xml" into xmlDoc using loadXMLDoc()
2. Set the variable y to be the element node to remove
3. Remove the element node by using the removeChild() method from the parent node

## Remove Myself - Remove the Current Node

The removeChild() method is the only way to remove a specified node.

When you have navigated to the node you want to remove, it is possible to remove that node using the parentNode property and the removeChild() method:

## Example

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0];
x.parentNode.removeChild(x);
Try it yourself »

Example explained:

1. Load "books.xml" into xmlDoc using loadXMLDoc()
2. Set the variable y to be the element node to remove
3. Remove the element node by using the parentNode property and the removeChild() method

## Remove a Text Node

The removeChild() method can also be used to remove a text node:

## Example

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0];
y=x.childNodes[0];
x.removeChild(y);
Try it yourself »

Example explained:

1. Load "books.xml" into xmlDoc using loadXMLDoc()
2. Set the variable x to be the first title element node
3. Set the variable y to be the text node to remove
4. Remove the element node by using the removeChild() method from the parent node

It is not very common to use removeChild() just to remove the text from a node. The nodeValue property can be used instead. See next paragraph.

## Clear a Text Node

The nodeValue property can be used to change or clear the value of a text node:

## Example

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
x.nodeValue="";
Try it yourself »

Example explained:

1. Load "books.xml" into xmlDoc using loadXMLDoc()
2. Set the variable x to be the text node of the first title element
3. Use the nodeValue property to clear the text from the text node

Loop through and change the text node of all <title> elements: Try it yourself

## Remove an Attribute Node by Name

The removeAttribute(name) method is used to remove an attribute node by its name.

Example: removeAttribute('category')

The following code fragment removes the "category" attribute in the first <book> element:

## Example

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
x[0].removeAttribute("category");
Try it yourself »

Example explained:

1. Load "books.xml" into xmlDoc using loadXMLDoc()
2. Use getElementsByTagName() to get book nodes
3. Remove the "category" attribute form the first book element node

Loop through and remove the "category" attribute of all <book> elements: Try it yourself

## Remove Attribute Nodes by Object

The removeAttributeNode(node) method is used to remove an attribute node, using the node object as parameter.

Example: removeAttributeNode(x)

The following code fragment removes all the attributes of all <book> elements:

## Example

xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book");
for (i=0;i<x.length;i++)
{
while (x[i].attributes.length>0)
{
attnode=x[i].attributes[0];
old_att=x[i].removeAttributeNode(attnode);
}
}
Try it yourself »

Example explained:

1. Load "books.xml" into xmlDoc using loadXMLDoc()
2. Use getElementsByTagName() to get all book nodes
3. For each book element check if there are any attributes
4. While there are attributes in a book element, remove the attribute

« Previous
Next Chapter »

### WEB HOSTING

UK Reseller Hosting

### WEB BUILDING

FREE Website BUILDER Free HTML5 Templates

### W3SCHOOLS EXAMS

HTML, CSS, JavaScript, PHP, jQuery, XML, and ASP Certifications

### SHARE THIS PAGE

### Color Picker

### HTML/CSS

HTML Tutorial HTML5 Tutorial CSS Tutorial CSS3 Tutorial Bootstrap Tutorial

### JavaScript

JavaScript Tutorial jQuery Tutorial jQuery Mobile Tutorial AngularJS Tutorial AJAX Tutorial JSON Tutorial

### Graphics

Canvas Tutorial SVG Tutorial Google Maps Tutorial

### Server Side

SQL Tutorial PHP Tutorial ASP Tutorial ASP.NET Tutorial VBScript Tutorial AppML Tutorial

### Web

Web Building Web Statistics Web Certification

### XML

XML Tutorial DTD Tutorial Schema Tutorial XML DOM Tutorial XPath Tutorial XSLT Tutorial XQuery Tutorial XSL-FO Tutorial RSS Tutorial WSDL Tutorial

### HTML/CSS

HTML Tag Reference HTML Event Reference HTML Color Reference CSS 1,2,3 Reference Bootstrap Reference

### JavaScript

JavaScript Reference HTML DOM Reference jQuery Reference jQuery Mobile Reference Google Maps Reference

### Server Side

PHP Reference SQL Reference ASP Reference VBScript Reference Razor Reference ASP.NET Reference

### XML

XML DOM Reference XSLT Reference XSL-FO Reference XPath Reference XQuery Reference Schema Reference RSS Reference SVG Reference

### HTML Charsets

HTML Character Sets HTML ASCII HTML ANSI HTML Windows-1252 HTML ISO-8859-1 HTML Symbols HTML UTF-8

### HTML/CSS

HTML Examples CSS Examples

### Browser Scripting

JavaScript Examples HTML DOM Examples jQuery Examples jQuery Mobile Examples AngularJS Examples AJAX Examples

### Server Scripting

PHP Examples ASP Examples VBScript Examples Razor Examples .NET Examples

### XML Languages

XML Examples DTD Examples XML DOM Examples XSL Examples XSLT Examples XPath Examples XQuery Examples Schema Examples RSS Examples SVG Examples